/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package util.tokenization_io;

import java.util.regex.Pattern;


import java.io.Serializable;

public class RegexTokenizer implements Tokenizer, Serializable {

    Normalizer norm;
    Pattern pattern;

    /**
     * Constructor
     *
     * @param regexFileName Location of file containing a regular expression
     * @param norm A Normalizer for lowercasing or other normalizations
     */
    public RegexTokenizer(String regexFileName, Normalizer norm) {
        //System.out.println(regexFileName);
        pattern = null;
        try {
            String s = Slurp.slurp(regexFileName);
            pattern = Pattern.compile(s, Pattern.COMMENTS);
            //System.out.println("Regex: " + s);
        } catch (Exception e) {
            System.out.println(e);
            System.exit(0);
        }

        this.norm = norm;
    }

    /**
     * Constructor without reset
     *
     * @param regexFileName Location of file containing a regular expression
     * @param norm A Normalizer for lowercasing or other normalizations
     * @param flag To distinguish this constructor
     */
    public RegexTokenizer(String regexFileName, Normalizer norm, boolean flag) {
        pattern = Pattern.compile(Slurp.slurp(regexFileName), Pattern.COMMENTS);
        this.norm = norm;
    }

    /**
     * Default constructor.
     */
    public RegexTokenizer() {
        pattern = Pattern.compile("[\\d\\D]");
        this.norm = new NoNormalize();
    }

    /**
     * 
     * @param s The text from which tokens will be extracted.
     * @return An Iterator yielding the tokens.
     */
    @Override
    public TokenIterator iterator(String s) {
        return new TokenIterator(pattern.matcher(s), norm);
    }

    /**
     * 
     * @param s The text from which tokens will be extracted.
     * @param flag To turn off reset of the Normalizer (obsolete)
     * @return An Iterator yielding the tokens.
     */
    public TokenIterator iterator(String s, boolean flag) {
        return new TokenIterator(pattern.matcher(s), norm);
    }
}
